/**
 * @file drv_gpio.c
 * @brief HAL CUBEMX DRIVER
 * 
 * @author dalin (dalin@open-robot.com)
 * @version 1.0
 * @date 2023-05-10
 * 
 * @copyright Copyright (c) 2023  Open Robot Tech.co, Ltd
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-05-10 <td>1.0     <td>dalin     <td>内容
 * </table>
 */

#include "drv_gpio.h"

#ifdef USE_ADC_DRIVER
#include "gpio.h"



static GPIO_TypeDef * get_gpio_type(gpioType_e gpiox)
{
    GPIO_TypeDef *GPIOx =NULL;

    switch (gpiox)
    {
    case DRV_GPIOA:
        GPIOx = GPIOA;
        break;
    case DRV_GPIOB:
        GPIOx = GPIOB;
        break;
    case DRV_GPIOC:
        GPIOx = GPIOC;
        break;
    case DRV_GPIOD:
        GPIOx = GPIOD;
        break;
    case DRV_GPIOE:
        GPIOx = GPIOE;
        break;
#if STM32FxxxRx
    case DRV_GPIOF:
        GPIOx = GPIOF;
        break;
    case DRV_GPIOG:
        GPIOx = GPIOG;
        break;
#endif
    default:
        break;
    }

    return GPIOx;
}


static uint16_t get_gpio_pin(gpioPin_e pin)
{
    uint16_t pin_ = 0;

    switch (pin)
    {
    case DRV_PIN_0: 
        pin_ = GPIO_PIN_0;
        break;
    case DRV_PIN_1: 
        pin_ = GPIO_PIN_1;
        break;
    case DRV_PIN_2: 
        pin_ = GPIO_PIN_2;
        break;
    case DRV_PIN_3: 
        pin_ = GPIO_PIN_3;
        break;
    case DRV_PIN_4: 
        pin_ = GPIO_PIN_4;
        break;
    case DRV_PIN_5: 
        pin_ = GPIO_PIN_5;
        break;
    case DRV_PIN_6: 
        pin_ = GPIO_PIN_6;
        break;
    case DRV_PIN_7: 
        pin_ = GPIO_PIN_7;
        break;
    case DRV_PIN_8: 
        pin_ = GPIO_PIN_8;
        break;
    case DRV_PIN_9: 
        pin_ = GPIO_PIN_9;
        break;
    case DRV_PIN_10: 
        pin_ = GPIO_PIN_10;
        break;
    case DRV_PIN_11: 
        pin_ = GPIO_PIN_11;
        break;
    case DRV_PIN_12: 
        pin_ = GPIO_PIN_12;
        break;
    case DRV_PIN_13: 
        pin_ = GPIO_PIN_13;
        break;
    case DRV_PIN_14: 
        pin_ = GPIO_PIN_14;
        break;
    case DRV_PIN_15: 
        pin_ = GPIO_PIN_15;
        break;
    case DRV_PIN_ALL: 
        pin_ = GPIO_PIN_All;
        break;
    
    default:
        break;
    }

    return pin_;
}


/**
 * @brief 设置IO状态
 * 
 * @param  drv              : GPIO设备
 * 
 */
void drv_gpio_write(gpioDrv_t drv, drvPinState_e ste)
{
    GPIO_TypeDef * gpio;
    uint16_t pin;

    gpio = get_gpio_type(drv.gpiox);
    pin  = get_gpio_pin(drv.pin);


    HAL_GPIO_WritePin(gpio, pin, (GPIO_PinState)ste);
}


/**
 * @brief 读取IO状态
 * 
 * @param  drv              : GPIO设备
 * 
 * @return drvPinState_e 
 */
drvPinState_e drv_gpio_read(gpioDrv_t drv)
{
    GPIO_TypeDef * gpio;
    uint16_t pin;

    gpio = get_gpio_type(drv.gpiox);
    pin  = get_gpio_pin(drv.pin);

    return (drvPinState_e)(HAL_GPIO_ReadPin(gpio, pin));
}


/**
 * @brief 
 * 
 * @param  drv              : GPIO设备
 * 
 */
void drv_gpio_toggle(gpioDrv_t drv)
{
    GPIO_TypeDef * gpio;
    uint16_t pin;

    gpio = get_gpio_type(drv.gpiox);
    pin  = get_gpio_pin(drv.pin);

    HAL_GPIO_TogglePin(gpio, pin);
}

#endif /* USE_ADC_DRIVER */